package Test;

import java.io.IOException;
import java.net.UnknownHostException;

import GUI.network.OfficeConnection;
import OfficeServer.log_error.UserException;
import OfficeServer.users.AccessLevel;
import OfficeServer.users.Patient;
import OfficeServer.users.PersonalInfo;
import OfficeServer.users.UserList;
import junit.framework.TestCase;

public class ServerTest extends TestCase
{
    /**
     * @author Chris Bayruns
     * @throws Exception
     */
    public void testLogin() throws Exception {
	OfficeConnection oTest = new OfficeConnection("127.0.0.1", 12345);

	try {
	    oTest.userLogin("admin", "admin1");
	} catch (Exception e) {
	    fail(e.getMessage());
	}
	assertTrue(oTest.isLoggedIn());

	// assertEquals(newPat, oTest.getUser());

	oTest.userLogout();

	assertFalse(oTest.isLoggedIn());
	oTest.disconnect();
    }
    
    /**
     * @author Chris Bayruns This test will test the 3 attempt lockout and a
     *         subsequent unlock.
     */
    /**
     * @author Chris Bayruns
     * @throws IOException
     * @throws UnknownHostException
     */
    public void testLoginThreeTryFail() throws UnknownHostException,
	    IOException {

	OfficeConnection oTest = new OfficeConnection("127.0.0.1", 12345);
	
	try {
	    oTest.userLogin("admin", "admin1");
	    oTest.addUser("Patient", "Lol", "test", "test1", AccessLevel.PATIENT);
	    assertTrue(oTest.isLoggedIn());
	} catch (Exception e) {
	    fail(e.getMessage());
	}
	
	
	// Three wrong passwords
	for (int i = 0; i < 3; i++) {
	    try {
		oTest.userLogin("test", "wrongPassword");
	    } catch (UserException e) {
		assertEquals(UserException.REASON.WRONG_PASSWORD, e.getReason());
	    } catch (Exception e) {
		fail("Login lockout fail");
	    }
	}
	// Now for the lockout
	try {
	    oTest.userLogin("test", "wrongPassword");
	} catch (UserException e) {
	    assertEquals(UserException.REASON.ACCOUNT_LOCKED, e.getReason());

	    // assertEquals(UserException.REASON.ACCOUNT_LOCKED, e.getReason());
	} catch (Exception e) {
	    fail("Login lockout fail");
	}

	// Now try with the correct password
	try {
	    oTest.userLogin("test", "test1");
	    assertFalse(oTest.isLoggedIn());
	} catch (UserException e) {
	    assertEquals(UserException.REASON.ACCOUNT_LOCKED, e.getReason());

	    // assertEquals(UserException.REASON.ACCOUNT_LOCKED, e.getReason());
	} catch (Exception e) {
	    fail("Login lockout fail");
	}
	
	try {
		oTest.userLogout();
	    oTest.userLogin("admin", "admin1");
	    oTest.userSearch("test");
	    PersonalInfo tempP = oTest.getSearchPersonalInfo();
	    tempP.resetAttempts();
	    oTest.setSearchPersonalInfo(tempP);
	    oTest.userLogout();
	} catch (Exception e) {
	    fail(e.getMessage());
	}

	// Now try with the correct password and the attempts reset
	try {
	    oTest.userLogin("test", "test1");
	    assertTrue(oTest.isLoggedIn());
	} catch (UserException e) {
	    fail("Login unlock fail");
	} catch (Exception e) {
	    fail("Login unlock fail");
	}

	oTest.disconnect();
    }
}
